package com.android.myapplication.view.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.myapplication.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class StudentAdapter extends BaseAdapter {
    Context mContext;
    List<StudentBean> mList = new ArrayList<>();
    public StudentAdapter(Context context){
        mContext = context;
    }

    public StudentAdapter(Context mContext, List<StudentBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(List<StudentBean> list){
        mList = list;
        notifyDataSetChanged();
        notifyDataSetInvalidated();
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position == 1){
            TextView textView = (TextView) LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, null);
            textView.setText("000000000000000000");
            return textView;
        }

        if (convertView == null || convertView instanceof TextView){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_student, null);
            convertView.setTag(new ViewHolder(convertView));
        }

        StudentBean bean = mList.get(position);
        ((ViewHolder)convertView.getTag()).setData(bean);
        return convertView;
    }


    class ViewHolder{
        TextView mTxName;
        TextView mTxClassName;
        TextView mTxSex;
        TextView mTxAge;
        ImageView mImgView;
        ViewHolder(View view){
            mTxName = view.findViewById(R.id.txt_name);
            mTxClassName = view.findViewById(R.id.txt_class);
            mTxSex = view.findViewById(R.id.txt_sex);
            mTxAge = view.findViewById(R.id.txt_age);
            mImgView = view.findViewById(R.id.image1);
        }

        public void setData(StudentBean bean){
            mTxName.setText(bean.name);
            mTxClassName.setText(bean.className);
            mTxSex.setText(bean.sex == 0 ? "男" : "女");
            mTxAge.setText(bean.age + "岁");
            mImgView.setImageURI(Uri.parse(bean.img));
        }
    }


}
